package team.pwy.controller;

import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team.pwy.pojo.entity.MovieIndex;
import team.pwy.service.MovieIndexService;
import team.pwy.vo.Result;
import team.pwy.vo.ResultES;
import team.pwy.vo.TypeVo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("movieIndex")
public class MovieESController {

    @Autowired
    private MovieIndexService movieIndexService;

    @GetMapping("/countryAndStatus")
    public ResultES findMovieByCountryES(TypeVo typeVo, Integer pageNum, Integer pageSize) throws IOException {

        SearchHits<MovieIndex> movieIndexSearchHits = movieIndexService.findMovieIndexByCountryAndStatus(typeVo, pageNum, pageSize);

        List<MovieIndex> movieIndexList = new ArrayList<>();
        List<List<String>> heightLight = new ArrayList<>();
        for (SearchHit<MovieIndex> hit : movieIndexSearchHits) {
            MovieIndex movieIndex = hit.getContent();
            movieIndexList.add(movieIndex);
            heightLight.add(hit.getHighlightField("title"));
        }

        return new ResultES("200", "", movieIndexList, heightLight, movieIndexSearchHits.getTotalHits());
    }
}
